package com.cq.hd.merchant.controller;

import com.cq.hd.common.annotation.MerchantLoginUser;
import com.cq.hd.common.entity.MerchantAppLoginUser;
import com.cq.hd.common.entity.MerchantUserInfo;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbBusinessMerchantApi;
import com.cq.hd.member.api.TbMerchantBankUpdateRecordApi;
import com.cq.hd.member.api.dto.MerchantBankCardDto;
import com.cq.hd.member.api.dto.MerchantCustomUpdateDto;
import com.cq.hd.member.api.dto.MerchantPwdUpdateDto;
import com.cq.hd.member.api.dto.MyMerchantBusinessPageDto;
import com.cq.hd.member.api.vo.MerchantBankCardVo;
import com.cq.hd.member.api.vo.MerchantPageVo;
import com.cq.hd.member.api.vo.MyMerchantBusinessPageVo;
import com.cq.hd.merchant.biz.MerchantBiz;
import com.cq.hd.merchant.dto.MerchantAvatarUpdateDto;
import com.cq.hd.merchant.dto.MerchantBankCardUpdateDto;
import com.cq.hd.merchant.dto.MerchantShortNameUpdateDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "商户相关接口")
public class MerchantController {

    @Autowired
    private MerchantBiz merchantBiz;

    @Autowired
    private TbBusinessMerchantApi businessMerchantApi;

    @Autowired
    private TbMerchantBankUpdateRecordApi tbMerchantBankUpdateRecordApi;

    @ApiOperation(value = "获取商户详细信息")
    @ApiResponse(response = MerchantUserInfo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/getMerchantInfo")
    public Result<MerchantUserInfo> getMerchantInfo(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return ResultGenerator.success(merchantBiz.getMerchantInfo(merchantAppLoginUser));
    }

    @ApiOperation(value = "我的策划人分页条件列表")
    @ApiResponse(response = MyMerchantBusinessPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageMyBusiness")
    public Result<Page<MyMerchantBusinessPageVo>> pageMyBusiness(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, MyMerchantBusinessPageDto myMerchantBusinessPageDto) {
        myMerchantBusinessPageDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        return businessMerchantApi.pageMyBusiness(myMerchantBusinessPageDto);
    }

    @ApiOperation(value = "我的策划人列表")
    @ApiResponse(response = MyMerchantBusinessPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listMyBusiness")
    public Result<List<MyMerchantBusinessPageVo>> listMyBusiness(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, MyMerchantBusinessPageDto myMerchantBusinessPageDto) {
        myMerchantBusinessPageDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        return businessMerchantApi.listMyBusiness(myMerchantBusinessPageDto);
    }

    @ApiOperation(value = "自有商户的商家关联的商户列表（不包含自己）")
    @ApiResponse(response = MerchantPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listMyBusinessMerchant")
    public Result<List<MerchantPageVo>> listMyBusinessMerchant(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return ResultGenerator.success(merchantBiz.listMyBusinessMerchant(merchantAppLoginUser.getMerchantId()));
    }

    @ApiOperation(value = "修改客服信息")
    @PutMapping("/updateCustomInfo")
    public Result<Boolean> updateCustomInfo(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                                     @RequestBody MerchantCustomUpdateDto merchantCustomUpdateDto) {
        merchantCustomUpdateDto.setId(merchantAppLoginUser.getMerchantId());
        return merchantBiz.updateCustomInfo(merchantCustomUpdateDto);
    }

    @ApiOperation(value = "修改登录密码")
    @PutMapping("/updatePwd")
    public Result<Boolean> updatePwd(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                            @RequestBody MerchantPwdUpdateDto merchantPwdUpdateDto) {
        merchantPwdUpdateDto.setId(merchantAppLoginUser.getMerchantId());
        return merchantBiz.updatePwd(merchantPwdUpdateDto);
    }

    @GetMapping("/bankCard")
    public Result<MerchantBankCardVo> getMerchantBankCard(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return ResultGenerator.success(merchantBiz.getMerchantBankCard(merchantAppLoginUser.getMerchantId()));
    }


    @PutMapping("/bankCard")
    public Result<Boolean> updateMerchantBankCard(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                                  @RequestBody MerchantBankCardUpdateDto merchantBankCardDto) {
        return ResultGenerator.success(merchantBiz.updateMerchantBankCard(merchantAppLoginUser, merchantBankCardDto));
    }

    @ApiOperation(value = "更新商户简称")
    @PutMapping("/updateShortName")
    public Result<Boolean> updateShortName(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                           @RequestBody MerchantShortNameUpdateDto merchantShortNameUpdateDto) {
        return merchantBiz.updateShortName(merchantAppLoginUser, merchantShortNameUpdateDto.getMerchantShortName());
    }

    @ApiOperation(value = "更新商户头像")
    @PutMapping("/updateAvatar")
    public Result<Boolean> updateAvatar(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                        @RequestBody MerchantAvatarUpdateDto merchantAvatarUpdateDto) {
        return merchantBiz.updateAvatar(merchantAppLoginUser, merchantAvatarUpdateDto.getAvatar());
    }

}
